var batteryTable;
/****列表信息*/
function showBatteries() {
	showLoading("查询中...");
	$.ajax( {
		url : 'battery/listPage.do',
		type : 'get',
		async:true,
		dataType : 'html',
		success : function(html) {
			$('#mainForm').html(html);
			queryBatterisDatas();
			hideLoading();
			//绑定查询按钮 
		$('#searchDianChiBtn').click(function() {
			queryBatterisPage();
		});
		$(".comboBox").kendoComboBox({filter:"contains", suggest:true });
	}
	});
}
function queryBatterisDatas(){
		batteryTable = $('#entTable').table( {
		width : '100%',
		defaultOrderBy:"sort",
		defaultOrderType:"asc",
		form : {
			url : 'comm/search.do',
			id : '#entSearchForm',
			type : 'post',
			pageNumberText : '#pageNum'
		},
		pageable : true,
		selectable : true,
		colums : [ {
			columnName : '电池品牌',
			name : 'pinPai',
			handle : "toAddBattery"
		}, {
			columnName : '电池型号',
			name : 'xingHao'
		}, {
			columnName : '市场换购价',
			name : 'marketPrice'
		}, {
			columnName : '本网换购价',
			name : 'ourPrice'
		}, {
			columnName : '创建时间',
			name : 'crateDate'
		}, {
			columnName : '更新时间',
			name : 'updateDate'
		}, {
					columnName : '显示状态',
					name : 'state',
					formatter:function(obj){
					if(obj.state=='1'){
						return "显示";
					}else if(obj.state=='0'){
						return "不显示";
					}else{
						return "";
					}
				}
				}]
	});
}
function queryBatterisPage() {
	showLoading("查询中...");
	queryBatterisDatas();
	hideLoading();
}
//去增加
function toAddBattery(id) {
	var param = '?t=' + ts();
	if (id) {
		param += '&id=' + id;
	}
	
	$.get('battery/toAddDianChi.do' + param, function(html) {
		$('#commonDialogDiv').html(html);
		$('#commonDialogDiv').dialog( {
			modal : true,
			title : '电池信息表',
			width : 995,
			height : 600,
			buttons : {
				'保存' : function() {
					saveAddBattery();
				},
				'取消' : function() {
					$(this).dialog('close');
				}
			},close:function(){$(this).dialog('destroy');}
		});
		addValidatorFloat();
		addValidatorInteger();
		validator = $("#DianChiForm").validate( {
			rules : {
				"model.name" : {
					required : true,
					maxlength : 20
				},
				"model.marketPrice":{
					required : true,
					FloatCheck:true
				},
				"model.ourPrice":{
					required : true,
					FloatCheck:true
				},
				"model.zhongliang":{
					FloatCheck:true
				},
				"model.licheng":{
					FloatCheck:true
				},
				"model.detailInfo":{
					maxlength : 150
				},
				"model.sort":{
					required : true,
					integer:true
				}
			},
			messages : {
				"model.name" : {
					required : "必填"
				},
				"model.marketPrice":{
					required : "必填",
					FloatCheck:"格式：整数位最多6位,小数位最多2位"
				},
				"model.ourPrice":{
					required : "必填",
					FloatCheck:"格式：整数位最多6位,小数位最多2位"
				},
				"model.zhongliang":{
					FloatCheck:"格式：数字,小数位最多2位,单位kg "
				},
				"model.licheng":{
					FloatCheck:"格式：数字,小数位最多2位,单位公里 "
				},
				"model.detailInfo":{
					maxlength : "最长不超过150字"
				},
				"model.sort":{
					required : "必填",
					integer : "只能填写大于0数字"
				}
			}
		});
	});
}
//去保存
function saveAddBattery() {
	if (validator.form()) {
		$('#DianChiForm').ajaxSubmit( {
			url : 'battery/saveDianChi.do',
			type : 'post',
			dataType : 'json',
			success : function(data) {
				if (data.success) {
					showDialog('保存成功');
					$('#commonDialogDiv').dialog('close');
					queryBatterisPage();
				} else {
					showDialog('保存失败，' + data.message);
				}
			}
		});
	}
}

//批量删除 
function toDeleteBattery() {
	var ids = '';
	$('.ids').each(function() {
		if (this.checked == true) {
			ids += 'ids=' + this.value + '&';
		}
	});
	if (ids.length == 0) {
		showDialog('请至少选择一条记录！');
		return;
	}
	$('#commonDialogDiv').html('<p>确定要删除记录信息？</p>');
	$('#commonDialogDiv').dialog( {
		modal : true,
		width : 250,
		height : 160,
		buttons : {
			'确定' : function() {
				$(this).dialog('close');
				$.ajax( {
					url : 'battery/delete.do?' + ids,
					type : 'get',
					dataType : 'json',
					success : function(data) {
						if (data.success) {
							queryBatterisPage();
						} else {
							showDialog('删除失败，原因：' + data.message);
						}
					}
				});
			},
			'取消' : function() {
				$(this).dialog('close');
			}
		},close:function(){$(this).dialog('destroy');}
	});
}
